package api

import (
	"context"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"user_bff/forms"
	"user_bff/global"
	"user_bff/pkg"
	user_pb "user_bff/user"
)

// 注册用户
func RegisterUser(c *gin.Context) {
	var form forms.RegisterUserForm

	if err := c.ShouldBind(&form); err != nil {
		ReturnErrorJson(err, c)
		return
	}
	user, err := global.SrvConn.CreateUser(context.Background(), &user_pb.CreateUserInfo{
		NickName: form.NickName,
		Mobile:   form.Mobile,
		Password: form.Password,
	})
	if err != nil {
		ReturnErrorJson(err, c)
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    0,
		"message": "注册成功",
		"data":    user,
	})
}

// 登录用户
func LoginUser(c *gin.Context) {
	var form forms.LoginUserForm

	if err := c.ShouldBind(&form); err != nil {
		ReturnErrorJson(err, c)
		return
	}
	//校验图形验证码是否正确
	flg := store.Verify(form.CaptchaId, form.Captcha, true)
	if !flg {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": -1,
			"msg":  "图形验证码错误",
			"data": nil,
		})
		return
	}
	//根据手机号查询用户信息
	mobile, err := global.SrvConn.GetUserByMobile(context.Background(), &user_pb.MobileRequest{Mobile: form.Mobile})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": -1,
			"msg":  "账号不存在",
			"data": nil,
		})
		return
	}
	//校验密码
	word, err := global.SrvConn.CheckPassWord(context.Background(), &user_pb.PasswordCheckInfo{
		Password:          form.Password,
		EncryptedPassword: mobile.PassWord,
	})
	if err != nil {
		ReturnErrorJson(err, c)
		return
	}
	if !word.Success {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 0,
			"msg":  "密码错误",
			"data": nil,
		})
		return
	}
	//生成token

	marshal, _ := json.Marshal(mobile)

	token, err := pkg.GetTokenPair(string(marshal), global.ServerConfig.JwtConfig.AccessTokenExpire, global.ServerConfig.JwtConfig.RefreshTokenExpire)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": -1,
			"msg":  "token生成失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "登录成功",
		"data": map[string]interface{}{
			"accessToken":        token.AccessToken,
			"refreshToken":       token.RefreshToken,
			"accessTokenExpire":  global.ServerConfig.JwtConfig.AccessTokenExpire,
			"refreshTokenExpire": global.ServerConfig.JwtConfig.RefreshTokenExpire,
		},
	})
}

// 获取用户列表
func GetUserList(c *gin.Context) {
	pn := c.Query("pn")
	size := c.Query("size")
	pnInt, _ := strconv.Atoi(pn)
	sizeInt, _ := strconv.Atoi(size)
	list, err := global.SrvConn.GetUserList(context.Background(), &user_pb.PageInfo{
		Pn:    uint32(pnInt),
		PSize: uint32(sizeInt),
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": -1,
			"msg":  "获取用户列表失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "success",
		"data": list,
	})
}

// 刷新令牌
func RefreshToken(c *gin.Context) {
	//获取请求头中的refresh_token
	refreshToken := c.PostForm("refresh_token")

	token, _ := pkg.RefreshAccessToken(refreshToken, 30)
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "刷新成功",
		"data": map[string]interface{}{
			"access_token":       token,
			"refresh_token":      refreshToken,
			"accessTokenExpire":  global.ServerConfig.JwtConfig.AccessTokenExpire,
			"refreshTokenExpire": global.ServerConfig.JwtConfig.RefreshTokenExpire,
		},
	})
}

func Health(c *gin.Context) {
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "要不要看看我的妙妙屋",
		"data": nil,
	})
}
